package com.tensquare.base.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.tensquare.base.pojo.Label;
import com.tensquare.base.service.LabelService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/label")
public class LabelController {
    @Autowired
    private LabelService labelService;

    @PostMapping
    public Result save(@RequestBody Label label){
        labelService.save(label);
        return new Result(true, StatusCode.OK,"ok");
    }
  //  GET/label 标签全部列表
    @GetMapping
    public Result findAll(){
        List<Label>labels=labelService.findAll();
        return new Result(true,StatusCode.OK,"ok",labels);
    }
  //  GET/label/toplist  推荐标签列表
  @GetMapping("/toplist")
  public Result findAllByToplist(){
        List<Label>labels=labelService.findByToplist();
        return new Result(true,StatusCode.OK,"ok",labels);
  }
    //GET/label/list 有效标签列表
    @GetMapping("/list")
    public Result findAllByList() {
        List<Label>labels=labelService.findByList();
        return new Result(true,StatusCode.OK,"ok",labels);
    }
   // GET/label/{labelId}根据ID查询
   @GetMapping("/{labelId}")
   public Result findByLabelId(@PathVariable String labelId) {
        Label label=labelService.findById(labelId);
       return new Result(true,StatusCode.OK,"ok",label);
   }
   // PUT/label/{labelId}修改标签
    @PutMapping("{labelId}")
    public Result findByLabelId(@PathVariable String labelId,@RequestBody Label label) {
        label.setId(labelId);
        labelService.updateByLabelId(label);
        return new Result(true,StatusCode.OK,"ok");
    }
    //  DELETE/label/{labelId}根据ID删除
    @DeleteMapping("{labelId}")
    public  Result deleteByLabelId(@PathVariable String labelId){
        labelService.deleteByLabelId(labelId);
        return new Result(true,StatusCode.OK,"ok");
    }
/*
* POST
/label/search/{page}/{size}
标签分页*/
    @PostMapping("/search/{page}/{size}")
    public Result findByPage(@PathVariable Integer page,
                             @PathVariable Integer size,
                             @RequestBody Map<String,Object> map
                             ){
        Page<Label> pageData=labelService.findByPage(map,page,size);
        PageResult<Label> pageResult=new PageResult<>(pageData.getTotal(),pageData.getRecords());
            return new Result(true,StatusCode.OK,"ok",pageResult);
        }
    /*
    * POST
    /label/search
    条件查询*/
    @PostMapping("/search")
    public Result findBySearch( @RequestBody Map<String,Object> map){
        List<Label>list=labelService.findBySearch(map);
        return new Result(true,StatusCode.OK,"ok",list);
}


}
